package com.nmgsr.ptmsapp.activity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ext.rtmp.RtmpDataSourceFactory;
import com.nmgsr.nettools.base.BaseActivity;
import com.nmgsr.ptmsapp.R;
import com.nmgsr.ptmsapp.databinding.VideoActivityBinding;

public class VideoActivity extends BaseActivity {
    private static final String TAG = "VideoActivity";
    private VideoActivityBinding mBinding;
    private ExoPlayer player;
    private boolean isFullscreen = false;
    @Override
    protected int getLayoutId() {
        return R.layout.video_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mBinding = VideoActivityBinding.inflate(getLayoutInflater());
        View view = mBinding.getRoot();
        setContentView(view);

        player = new ExoPlayer.Builder(this).build();
        mBinding.playerView.setPlayer(player);

        // 使用 RtmpDataSourceFactory
        RtmpDataSourceFactory dataSourceFactory = new RtmpDataSourceFactory();
        MediaItem mediaItem = MediaItem.fromUri("https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4");
        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();

        mBinding.fullscreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleFullscreen();
            }
        });
    }
    private void toggleFullscreen() {
        if (isFullscreen) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mBinding.playerView.setLayoutParams(new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT,
                    600));
            //((ConstraintLayout.LayoutParams) mBinding.playerView.getLayoutParams()).setDimensionRatio("H,0.33");
            ((ConstraintLayout.LayoutParams) mBinding.playerView.getLayoutParams()).topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
            ((ConstraintLayout.LayoutParams) mBinding.playerView.getLayoutParams()).startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
            ((ConstraintLayout.LayoutParams) mBinding.playerView.getLayoutParams()).endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
            mBinding.fullscreenButton.setImageResource(R.drawable.baseline_aspect_ratio_24);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mBinding.playerView.setLayoutParams(new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT,
                    ConstraintLayout.LayoutParams.MATCH_PARENT));
            mBinding.fullscreenButton.setImageResource(R.drawable.baseline_fit_screen_24);
        }
        mBinding.playerView.requestLayout();
        isFullscreen = !isFullscreen;
    }
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.release();
            player = null;
        }
    }
}
